package org.linlinjava.litemall.admin.task;

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.linlinjava.litemall.admin.job.ShopMerchandiseJob;
import org.linlinjava.litemall.db.domain.LitemallShopMerchandiseJob;
import org.linlinjava.litemall.db.service.LitemallShopMerchandiseJobService;
import org.quartz.Scheduler;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * 店商品重新加载工作
 * Admin服务重新启动门店货品定时任务
 * @author macmini
 * @date 2022/12/04
 */
@Component
@RequiredArgsConstructor
public class ShopMerchandiseReloadJob implements ApplicationRunner {

    private final LitemallShopMerchandiseJobService shopMerchandiseJobService;

    private final CronTaskRegistrar registrar;

    private final Scheduler scheduler;

    @SneakyThrows(Exception.class)
    @Override
    public void run(ApplicationArguments args){
        System.out.println("===============Admin服务重新启动门店货品定时任务================");
        List<LitemallShopMerchandiseJob> litemallShopMerchandiseJobs = shopMerchandiseJobService.findStatus();
        if(CollUtil.isNotEmpty(litemallShopMerchandiseJobs)){
            litemallShopMerchandiseJobs.stream().forEach(litemallShopMerchandiseJob -> {
                registrar.schedulerAdd(litemallShopMerchandiseJob);
            });
        }
        // 恢复所有已暂停的任务
        scheduler.resumeAll();
    }
}
